package ro.ubbcluj.cs.damate.plugin.services;

import ro.ubbcluj.cs.damate.plugin.ui.dialogs.LoginDialog;
import ro.ubbcluj.cs.damate.plugin.utils.SavedDataUtils;
import ro.ubbcluj.cs.damate.plugin.utils.SpringUtils;
import ro.ubbcluj.cs.damate.plugin.ws.generated.AuthenticationResponse;
import ro.ubbcluj.cs.damate.plugin.ws.service.WebServiceClient;

import java.util.Calendar;
import java.util.Date;

public final class AuthenticationService {

    private SavedDataUtils savedDataUtils;

    private boolean authenticated;
    private String authenticationToken;

    private boolean loginDialogActive;
    private boolean authenticationCancelled;
    private Date lastCancelAction;

    public AuthenticationService() {
        this.authenticated = false;
        loadTokenFromCache();
    }

    private void loadTokenFromCache() {
        this.savedDataUtils = (SavedDataUtils) SpringUtils.getBean("savedDataUtils");
        if (savedDataUtils != null && savedDataUtils.hasAuthenticationToken()) {
            this.authenticationToken = savedDataUtils.getAuthenticationToken();
        }
    }

    public int authenticate(String username, String password) {
        AuthenticationResponse response = ((WebServiceClient) SpringUtils.getBean("webServiceClient")).authenticate(username, password);
        if (response.getResponseStatusCode() == 200) {
            this.authenticated = true;
            this.authenticationToken = response.getToken();
            savedDataUtils.setAuthenticationToken(authenticationToken);
            return response.getResponseStatusCode();
        } else if (response.getResponseStatusCode() == 404) {
            return response.getResponseStatusCode();
        } else if (response.getResponseStatusCode() == 401) {
        		return response.getResponseStatusCode();
        }
        return 500;
    }

    public void showLoginDialog() {
        LoginDialog.getLoginDialog(this).open();
        loginDialogActive = true;
    }

    public void onLoginDialogClose() {
        this.lastCancelAction = new Date();
        this.authenticationCancelled = true;
        this.loginDialogActive = false;
    }

    public boolean isAuthenticated() {
        return authenticated;
    }

    private boolean isLastCancelExpired() {
        if (lastCancelAction != null) {
            Calendar halfAnHourBefore = Calendar.getInstance();
            halfAnHourBefore.add(Calendar.MINUTE, -30);
            return lastCancelAction.before(halfAnHourBefore.getTime());
        }
        return false;
    }
    
    private boolean hasValidToken() {
    		if (authenticationToken != null) {
    				boolean tokenValid = ((WebServiceClient) SpringUtils.getBean("webServiceClient")).validateToken(authenticationToken);
    				this.authenticated = tokenValid;
    				return tokenValid;
    		}
    		return false;
    }

    public boolean isShowLoginDialog() {
        return !loginDialogActive && !authenticated && !hasValidToken() && (!authenticationCancelled || isLastCancelExpired());
    }

    public String getAuthenticationToken() {
        return authenticationToken;
    }
}
